package com.igeek.clud_system.service;

import com.igeek.clud_system.dao.*;
import com.igeek.clud_system.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClubService {
    @Autowired
    ClubDao clubDao;

    @Autowired
    ActivityDao activityDao;

    @Autowired
    NewsDao newsDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    AnnouncementDao announcementDao;

    @Autowired
    InformationDao informationDao;

    @Autowired
    ClubMembershipRequestDao requestDao;

    @Autowired
     ClubMemberDao clubMemberDao;

    Integer pageSize = 8;

    /**
     * 社团首页查询
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public List<Club> findClub(String name,int type,Integer statue){
        if(name.equals("DataNull") || name.equals("undefined")){
            name = null;
        }
        List<Club> club = null;
        if(type == 0){
            club = clubDao.findClub(null, name,statue);
        }else {
            club = clubDao.findClub(type, name,statue);
        }

        return club;
    }

    /**
     *通过Id查询社团具体信息
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> findClubById(Integer clubId){
        Map<String,Object> map = new HashMap<>();
        Club clubById = clubDao.findClubById(clubId);
        map.put("club",clubById);
        List<Activity> actByClubId = activityDao.findActByClubId(clubId);
        map.put("activity",actByClubId);
        List<ClubNews> newsBuClubId = newsDao.findNewsBuClubId(clubId);
        map.put("news",newsBuClubId);
        List<Student> studentByClubId = studentDao.findStudentByClubId(clubId);
        map.put("students",studentByClubId);
        return map;
    }

    /**
     * 获取活动首页
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> findClubMain(Integer clubId){
        Map<String,Object> map = new HashMap<>();
        Club clubById = clubDao.findClubById(clubId);
        map.put("club",clubById);
        List<Announcement> annByClubId = announcementDao.findAnnByClubId(clubId);
        map.put("announcement",annByClubId);
        return map;
    }

    /**
     * 对学生进行管理
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public List<Student> findAllStudent(Integer club){
        List<Student> studentByClubId = studentDao.findStudentByClubId(club);
        return studentByClubId;
    }

    /**
     * 获取社团内学生身份
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public Student getMan(Integer clubId,Integer studentId){
        Student clubAndStu = studentDao.findClubAndStu(clubId, studentId);
        System.out.println(clubAndStu+"my");
        return clubAndStu;
    }

    /**
     * 同意申请
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public void approve(Integer requestId){
        ClubMembershipRequests requestById = requestDao.findRequestById(requestId);
        String value = null;
        Club clubById = clubDao.findClubById(requestById.getClubId());
        if(requestById.getActionType() == 1){
            value="您的加入申请已被"+clubById.getClubName()+"同意";
            clubMemberDao.insertStudent(requestById.getClubId(),requestById.getStudentId());
            informationDao.insertInfor(requestById.getStudentId(),value,new Date(),1);
        }else {
            value = "您的退出申请已被"+clubById.getClubName()+"同意";
            clubMemberDao.delClub(requestById.getClubId(),requestById.getStudentId());
            informationDao.insertInfor(requestById.getStudentId(),value,new Date(),1);
        }
        requestDao.setRequest(2,requestId);
    }

    /**
     * 拒绝
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public void reject(Integer requestId){
        ClubMembershipRequests requestById = requestDao.findRequestById(requestId);
        String value = null;
        Club clubById = clubDao.findClubById(requestById.getClubId());
        if(requestById.getActionType() == 1) {
            value = "您的加入申请已被" + clubById.getClubName() + "拒绝";
            informationDao.insertInfor(requestById.getStudentId(),value,new Date(),1);
        }else {
            value = "您的退出申请已被"+clubById.getClubName()+"拒绝";
            informationDao.insertInfor(requestById.getStudentId(),value,new Date(),1);
        }
        requestDao.setRequest(3,requestId);
    }

    /**
     * 获取所有信息
     */
    public List<Information> infor(Integer studentId){
        List<Information> information = informationDao.selectAll(studentId);
        return information;
    }
}
